package com.cy.store.controller;

import com.cy.store.entity.Cart;
import com.cy.store.service.ICartService;
import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.util.JsonResult;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/carts")
public class CartController extends BaseController {
    @Autowired
    private ICartService cartService;

    @RequestMapping("/add_to_cart")
    public JsonResult<Void> addToCart(Integer pid, Integer amount, HttpSession session) {
        // 从Session中获取uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务对象执行添加到购物车
        cartService.addToCart(uid, pid, amount, username);
        // 返回成功
        return new JsonResult<>(OK);
    }

    @GetMapping({"", "/"})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session) {
        // 从Session中获取uid
        Integer uid = getUidFromSession(session);
        // 调用业务对象执行查询数据
        List<CartVO> data = cartService.getVOByUid(uid);
        // 返回成功与数据
        return new JsonResult<List<CartVO>>(OK, data);
    }

    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session) {
        // 从Session中获取uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务对象执行增加数量
        Integer data = cartService.addNum(cid, uid, username);
        // 返回成功
        return new JsonResult<Integer>(OK, data);
    }

    @RequestMapping("{cid}/num/sub")
    public JsonResult<Integer> subNum(@PathVariable("cid") Integer cid, HttpSession session, HttpServletRequest request) {
        // 从Session中获取uid和username
        Integer uid = getUidFromSession(session);
        //Cart cart = cartService.selectByCid(cid);
        //Integer num = cart.getNum();
        String username = getUsernameFromSession(session);
        //request.setAttribute("num",num);
        // 调用业务对象执行减少数量
        Integer data = cartService.subNum(cid, uid, username);
        // 返回成功
        return new JsonResult<Integer>(OK, data);
    }


    @GetMapping("/list")
    public JsonResult<List<CartVO>> getVOByCids(Integer[] cids, HttpSession session) {
        // 从Session中获取uid
        Integer uid = getUidFromSession(session);
        // 调用业务对象执行查询数据
        List<CartVO> data = cartService.getVOByCids(uid, cids);
        // 返回成功与数据
        return new JsonResult<>(OK, data);
    }

    @RequestMapping("{cid}/delete_to_cart")
    public JsonResult<Void> deleteToCart(HttpSession session,@PathVariable("cid") Integer cid){
        //根据session获取uid
        Integer uid = getUidFromSession(session);
        //删除购物车
        cartService.removeByCid(cid,uid);
        return new JsonResult<>(OK);

    }

    //根据cid批量删除购物车中的数据
    @RequestMapping("/deleteToCids")
    public JsonResult<Void> deleteToCids(Integer[] cids){
       /* Integer uid = getUidFromSession(session);
        for (Integer cid : cids) {
            cartService.removeByCid(cid,uid);
        }*/
        cartService.removeByCids(cids);
        return new JsonResult<>(OK);
    }
}